package org.bigmarketapi.gatewaycenter.trigger.http;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.bigmarketapi.gatewaycenter.application.IRegisterManage;
import org.bigmarketapi.gatewaycenter.domain.registry.model.aggre.appRegistryAggregate;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppInterfaceMethodVO;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppInterfaceVO;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppSystemVO;
import org.bigmarketapi.gatewaycenter.domain.registry.service.IRegistryImpl;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.common.ResponseCode;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.common.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: Gateway-Center
 * @BelongsPackage: org.bigmarketapi.gatewaycenter.trigger.http
 * @Author: dujiaqi
 * @CreateTime: 2024-12-08  14:18
 * @Description: TODO
 * @Version: 1.0
 */

@RestController
@RequestMapping("/wg/admin/register/")
@Slf4j
public class RPCRegistryManage implements IRegisterManage {
    IRegistryImpl registryService;

    public RPCRegistryManage(IRegistryImpl registryService) {
        this.registryService = registryService;
    }

    @Override
    @PostMapping(value = "registryApplication", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplication(@RequestParam String systemId, @RequestParam String systemName, @RequestParam String systemType, @RequestParam String systemRegistry) {
        try {
            final AppSystemVO appSystemVO = AppSystemVO.builder().systemId(systemId).systemName(systemName).systemRegistry(systemRegistry).systemType(systemType).build();
            registryService.registerApplication(appSystemVO);
            return Result.<Boolean>builder().code(ResponseCode.SUCCESS.getCode()).info(ResponseCode.SUCCESS.getInfo()).value(true).build();
        } catch (DuplicateKeyException e) {
            return Result.<Boolean>builder().code(ResponseCode.INDEX_DUP.getCode()).info(e.getMessage()).value(false).build();
        }
    }

    @Override
    @PostMapping(value = "registryApplicationInterface", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplicationInterface(@RequestParam String systemId, @RequestParam String interfaceId, @RequestParam String interfaceName, @RequestParam String interfaceVersion) {
        try {
            final AppInterfaceVO interfaceVO = AppInterfaceVO.builder().systemId(systemId).interfaceId(interfaceId).interfaceName(interfaceName).interfaceVersion(interfaceVersion).build();
            registryService.registerApplicationInterface(interfaceVO);
            return Result.<Boolean>builder().code(ResponseCode.SUCCESS.getCode()).info(ResponseCode.SUCCESS.getInfo()).value(true).build();
        } catch (DuplicateKeyException e) {
            return Result.<Boolean>builder().code(ResponseCode.INDEX_DUP.getCode()).info(e.getMessage()).value(false).build();
        }

    }

    @Override
    @PostMapping(value = "registryApplicationInterfaceMethod", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerApplicationInterfaceMethod(@RequestParam String systemId, @RequestParam String interfaceId, @RequestParam String methodId, @RequestParam String methodName, @RequestParam String parameterType, @RequestParam String uri, @RequestParam String httpType, @RequestParam Integer auth) {
        try {
            final AppInterfaceMethodVO methodVO = AppInterfaceMethodVO.builder().systemId(systemId).interfaceId(interfaceId).methodId(methodId).methodName(methodName).parameterType(parameterType).uri(uri).httpType(httpType).auth(auth).build();
            registryService.registerApplicationInterfaceMethod(methodVO);
            return Result.<Boolean>builder().code(ResponseCode.SUCCESS.getCode()).info(ResponseCode.SUCCESS.getInfo()).value(true).build();
        } catch (DuplicateKeyException e) {
            return Result.<Boolean>builder().code(ResponseCode.INDEX_DUP.getCode()).info(e.getMessage()).value(false).build();
        }
    }

    @Override
    @PostMapping(value = "queryAppRegistry", produces = "application/json;charset=utf-8")
    public Result<appRegistryAggregate> queryAppRegistry(@RequestParam String gatewayId) {
        try {
            final appRegistryAggregate aggregate = registryService.queryAppRegistry(gatewayId);
            log.info("查询汇总信息: {}", JSON.toJSONString(aggregate));
            return Result.<appRegistryAggregate>builder().code(ResponseCode.SUCCESS.getCode()).info(ResponseCode.SUCCESS.getInfo()).value(aggregate).build();
        } catch (Exception e) {
            return Result.<appRegistryAggregate>builder().code(ResponseCode.UN_ERROR.getCode()).info(e.getMessage()).value(null).build();
        }
    }
}
